import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "path";
import px2rem from "postcss-px2rem";
// import px2rem from "postcss-plugin-px2rem";
import postcssPxToViewport from "postcss-px-to-viewport";
// import postcssPxToViewport from "./src/utils/postcss-px-to-viewport.js";

// 引入svg需要用到的方法
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

// 引入Unocss
import Unocss from "unocss/vite";
import { presetUno, presetAttributify, presetIcons } from "unocss";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  //获取各种环境下的对应的变量
  let env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      Unocss({
        // 使用Unocss
        presets: [presetUno(), presetAttributify(), presetIcons()],
      }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
        symbolId: "icon-[dir]-[name]",
        customDomId: "__svg__icons__dom__",
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src"), // 相对路径别名配置，使用 @ 代替 src
      },
    },
    // scss全局变量的配置
    css: {
      // loaderOptions: {
      // postcss: {
      //   plugins: [
      //     px2rem({
      //       remUnit: 192,
      //       propList: ["*"],
      //       exclude: /src\/.*/,
      //     }),
      //   ],
      // },
      // },
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },

      postcss: {
        plugins: [
          px2rem({
            remUnit: 192,
          }),
          postcssPxToViewport({
            viewportWidth: 1920, //---基于1920宽度为100vw
            // exclude: [/service\.vue/]
            exclude: [/\/service\//, /service\/index\.vue$/],
          }),
        ],
      },
    },
    build: {
      outDir: "video_dist", //指定输出路径
      assetsDir: "assets", // 指定生成静态资源的存放路径
    },
    //代理跨域
    server: {
      host: "0.0.0.0",
      port: 8081,
      publicPath: process.env.NODE_ENV === "production" ? "./" : "/",
      proxy: {
        // '/api': {
        //   target: 'http://127.0.0.1:7001',
        //   changeOrigin: true,
        //   rewrite: (path) => path.replace(/^\/api/, ''),
        //   logLevel: 'debug',
        // },
        [env.VITE_APP_BASE_API]: {
          //获取数据的服务器地址设置
          target: env.VITE_SERVE,
          //需要代理跨域
          changeOrigin: true,
          logLevel: "debug",
          //路径重写
          // rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  };
});
